package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"migration-tool/common"
	"migration-tool/schedule"
	"os"
	"time"
)

var write bool

func init() {
	rootCmd.AddCommand(checkCmd)
	checkCmd.PersistentFlags().BoolVar(&write, "write", false, "默认false，为true时将导入失败的数据写入本地")
	checkCmd.PersistentFlags().BoolVar(&Increment, "increment", false, "默认false，为true时测试增量导入的数据")
}

/**
 * @Description:在完成导入之后，针对数据进行check
 */
var checkCmd = &cobra.Command{
	Use:   "check [--config=filepath][--write][--increment]",
	Short: "执行数据的校验逻辑",
	Run: func(cmd *cobra.Command, args []string) {
		if ConfigFilePath == "" {
			ConfigFilePath = common.GlobalDefaultConfigPath
		}

		coordinatorArgs := schedule.CoordinatorArgs{
			ConfigPath:    ConfigFilePath,
			Export:        false, // check属于import之后才执行的工作
			UseProgress:   true,
			Check:         true,
			Write:         write,
			IncrementFlag: Increment,
		}

		c, err := schedule.MakeCoordinator(coordinatorArgs)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		if c != nil {
			err := c.Start()
			if err != nil {
				return
			}
			for !c.Done() {
				time.Sleep(time.Second * 5)
			}

			c.RecordLog("[校验]工作完成", nil)
		}
		return
	},
}
